package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Objects;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 底图生成订单表Controller
 *
 * @author gs
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/system/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询底图生成订单表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Orders orders) {
        startPage();
        if (!SecurityUtils.isAdmin(getUserId())) {
            orders.setUserId(getUserId());
        }
        List<OrderDto> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }


    /**
     * 导出底图生成订单表列表
     */
    @Log(title = "底图生成订单表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders) {
        List<OrderDto> list = ordersService.selectOrdersList(orders);
        ExcelUtil<OrderDto> util = new ExcelUtil<OrderDto>(OrderDto.class);
        util.exportExcel(response, list, "底图生成订单表数据");
    }

    /**
     * 获取底图生成订单表详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return success(ordersService.selectOrdersByOrderId(orderId));
    }


    @PostMapping(value = "/storeOrder")
    public TableDataInfo getInfoByStoreId(@RequestParam("storeId") String storeId) {
        return getDataTable(ordersService.selectOrdersByStoreId(storeId));

    }

    /**
     * 完成订单
     * @param orderId
     * @return
     */
    @PostMapping(value = "/over")
    public AjaxResult over(@RequestParam("orderId") String orderId) {
        return toAjax(ordersService.over(orderId));
    }


    /**
     * 新增底图生成订单表
     */
    @Log(title = "底图生成订单表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders) {
        Long userId = SecurityUtils.getUserId();
        orders.setUserId(userId);
        orders.setCreateTime(DateUtils.getNowDate());
        if (Objects.equals(orders.getOrderStatus(), '1')) {
            Random random = new Random();
            int number = random.nextInt(90000000) + 10000000;
            orders.setPaymentNum(String.valueOf(number));
        }
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改底图生成订单表
     */
    @Log(title = "底图生成订单表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders) {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除底图生成订单表
     */
    @Log(title = "底图生成订单表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIdIds}")
    public AjaxResult remove(@PathVariable Long[] orderIdIds) {
        return toAjax(ordersService.deleteOrdersByOrderIds(orderIdIds));
    }
}
